<?php
/**
 * 产品属性管理
 * @author cjli
 *
 */
class AttributeAction extends AdminAction
{
	public function _initialize()
	{
		parent::_initialize();
		if(isset($_GET['modelid']) && $_GET['modelid']){
			$modelid = intval($_GET['modelid']);
			$this->assign('modelid', $modelid);
			$modelList = AttributeModel::getModelList();//属性类型列表
			$this->assign('model_name', $modelList[$modelid]);
		}
	}


	//类型列表
	public function model_list()
	{
	  $modeldb = D('model');
	  $modelList = $modeldb->field('model.*,count(attribute.attr_id) as model_count')->join('attribute ON attribute.model_id = model.id')->group('model.id')->select();
	  $this->assign('modelList', $modelList);
		$this->display('model_list');
	}

	//添加类型
	public function model_add()
	{
		$modeldb = D('model');
		if(isset($_POST['dosubmit'])){
			unset($_POST['dosubmit']);
			$modeldb->create();
			$lastid = $modeldb->add();
			if($lastid){
				S('model_list', null);
				$this->success('产品类型添加成功！', U('attribute/model_list'), 'add');
			}else{
				$this->error('产品类型添加失败！', U('attribute/model_list'), 'add');
			}
		}else{
			$this->assign('model', array());
			$this->display('model_form');
		}
	}

	//编辑类型
	public function model_edit()
	{
		$modelid = intval($_GET['modelid']);
		$modeldb = D('model');
		if(isset($_POST['dosubmit'])){
			unset($_POST['dosubmit']);
			$modeldb->create();
			$modeldb->where('id='.$modelid)->save();
			S('model_list', null);
			$this->success('产品类型编辑成功！', U('attribute/model_list'), 'edit');
		}else{
			$model_info = $modeldb->where('id='.$modelid)->find();
			$this->assign('model', $model_info);
			$this->display('model_form');
		}
	}

	//删除类型
	public function model_delete()
	{
		$modelid = intval($_GET['modelid']);
		$modeldb = D('model');
	  $modeldb->delete($modelid);
	  S('model_list', null);
		$this->success('产品类型删除成功！');
	}

	//属性列表
	public function attribute_list()
	{
		$modelid = intval($_GET['modelid']);
		$attributeList = AttributeModel::getAdminAttributeList($modelid);
		$this->assign('attributeList', $attributeList);
		$this->display('attribute_list');
	}

	//添加属性
	public function attribute_add()
	{
		$modelid = intval($_GET['modelid']);
		$model_id_arr = array(30,31,32);
		$is_show_sd = false;
		if(in_array($modelid,$model_id_arr)){
			$is_show_sd = true;
		}
		$this->assign('is_show_sd', $is_show_sd);
		if(isset($_POST['dosubmit']))
		{
			$attribute_id = AttributeModel::updateAttriubte($_POST);
			if($attribute_id){
				//属性内容详细入库
				AttributeModel::updateAttributeItems($_POST['attribute'], $attribute_id);

				$this->success('属性添加成功！', U('attribute/attribute_list', 'modelid='.$modelid));
			}else{
				$this->error('属性添加失败！', U('attribute/attribute_list', 'modelid='.$modelid));
			}
		}else{
			$attr_groups =  AttributeModel::getAttributeGroups($modelid);
			$this->assign('attr_groups',$attr_groups);
			$this->display('attribute_form');
		}
	}
	//编辑属性
	public function attribute_edit()
	{
		$modelid = intval($_GET['modelid']);
		$model_id_arr = array(30,31,32);
		$is_show_sd = false;
		if(in_array($modelid,$model_id_arr)){
			$is_show_sd = true;
		}
		$this->assign('is_show_sd', $is_show_sd);
		
		$attribute_id = intval($_GET['attrid']);
		$attrdb = D('attribute');
		$attrInfo = $attrdb->find($attribute_id);
		if(is_null($attrInfo)){
			$this->error('属性不存在！', U('attribute/attribute_list', 'modelid='.$modelid));
		}

		if(isset($_POST['dosubmit'])){
			$attribute_id = AttributeModel::updateAttriubte($_POST, $attribute_id);
			//对应的产品也默认成隐藏
			/**
			if($attribute_id && !empty($_POST['model_id'])){
				if(trim($_POST['is_ll'])==2){
					$model_id = intval($_POST['model_id']);
					
					$products=D("product")->where(array("model_id"=>$model_id))->select();
					foreach($products as $v){
						if(empty($v['show_attr_ids'])){
							D("product")->where(array("id"=>$v['id']))->save(array("show_attr_ids"=>$attribute_id));
						}else{
							$show_attr_array = explode(",",$v['show_attr_ids']);
							array_push($show_attr_array,$attribute_id);
							D("product")->where(array("id"=>$v['id']))->save(array("show_attr_ids"=>implode(",",$show_attr_array)));
						}
					}
				}else{
					$model_id = intval($_POST['model_id']);
					
					$products=D("product")->where(array("model_id"=>$model_id))->select();
					foreach($products as $v){
						if(!empty($v['show_attr_ids'])){
							$show_attr_array = explode(",",$v['show_attr_ids']);
							if(in_array($attribute_id,$show_attr_array)){
								$key=array_search($attribute_id,$show_attr_array);
								unset($show_attr_array[$key]);
								if(count($show_attr_array)==0){
									D("product")->where(array("id"=>$v['id']))->save(array("show_attr_ids"=>""));
								}else{
									D("product")->where(array("id"=>$v['id']))->save(array("show_attr_ids"=>implode(",",$show_attr_array)));
								}
							}
						}
					}
				}
			}**/
			if($attribute_id){
				//属性内容详细入库
				AttributeModel::updateAttributeItems($_POST['attribute'], $attribute_id);
				$this->success('属性编辑成功！', U('attribute/attribute_list', 'modelid='.$modelid));
			}else{
				$this->error('属性编辑失败！', U('attribute/attribute_list', 'modelid='.$modelid));
			}
		}else{
			$attrInfo['items'] = AttributeModel::getAttributeItems($attribute_id);
			$attr_groups =  AttributeModel::getAttributeGroups($modelid);
			$this->assign('attr_groups',$attr_groups);
			$this->assign('attrInfo', $attrInfo);
			$this->display('attribute_form');
		}
	}

	//删除属性
	public function attribute_delete()
	{
		$attribute_id = intval($_GET['attrid']);
		$modelid = intval($_GET['modelid']);
		AttributeModel::deleteAttriubte($attribute_id);
		$this->success('属性删除成功！', U('attribute/attribute_list', 'modelid='.$modelid));
	}
	
	public function plists(){
		$post = $_REQUEST;
	
		$pids_arr	= explode(",",$post['pids']);
		$attr_id	= $post['attr_id'];
		foreach($pids_arr as $v){
			$product =D("product_attribute")->where(array("is_default"=>$attr_id,"product_id"=>$v))->find();
			if($product){
				
				$id_new = $product_attr_id_new = $attr_value_id_new = array();
				$product_attr_res = D("product_attribute")->field("product_attr_id,attr_value_id")->where(array("attr_id"=>$product['attr_id'],"product_id"=>$v))->select();
				if(isset($product_attr_res) && $product_attr_res){
					foreach($product_attr_res as $val){
						array_push($product_attr_id_new,$val['product_attr_id']); 
						array_push($attr_value_id_new,$val['attr_value_id']);
					}
					$attr			  = D("attribute_values")->where(array("id"=>$attr_id))->find();
					$attr_res 		  = D("attribute_values")->field("id")->where(array("attr_id"=>$attr['attr_id']))->select();
				
					if(isset($attr_res) && $attr_res){
						foreach($attr_res as $va){
							array_push($id_new,$va['id']);
						}
						$result=array_intersect($attr_value_id_new,$id_new);
						if(isset($result) && $result){
							$key= array_search($result[0],$attr_value_id_new);
							if(isset($product_attr_id_new[$key]) && $product_attr_id_new[$key]){
								D("product_attribute")->where(array("product_attr_id"=>$product_attr_id_new[$key]))->save(array("is_default"=>$result[0]));
							}
							//删除默认
							D("product_attribute")->where(array("is_default"=>$attr_id,"product_id"=>$v))->delete();
						}
					}
				} 
			}
		}
		$this->success('对应产品默认设置成功！', U('attribute/attribute_values_list', array('modelid'=>$post['modelid'],'attrid'=>$post['attrid'])));
		
	}
	//属性值列表
	public function attribute_values_list()
	{
		$language_id = C('language_id');
		$attribute_id = intval($_GET['attrid']);
		$modelid = intval($_GET['modelid']);
		//属性
		$attribute = AttributeModel::getAttriubteInfo($attribute_id);
		$this->assign('attribute', $attribute);

		//属性值列表
		$attvaldb = D('attribute_values');
		$attvaldb->join('attribute_values_items ON attribute_values_items.att_value_id = attribute_values.id');
		$attributeValuesList =$attvaldb->where('attr_id='.$attribute_id.' AND attribute_values_items.language_id='.$language_id)->order('sort_order DESC, id ASC')->select();
		foreach($attributeValuesList as &$v){
			$product =D("product_attribute")->field('product_attribute.product_id')->join('product ON product_attribute.product_id = product.id')->where(array("product_attribute.is_default"=>$v['id'],"product.is_delete"=>0))->select();
			
			$v['product_ids'] = "";
			$array = array();
			foreach($product as $vp){
				array_push($array,$vp['product_id']);
			}
			if(count($array)>0){
				$v['product_ids'] =implode(",",$array);
			}
			$products =D("product_attribute")->field('product_attribute.product_id')->join('product ON product_attribute.product_id = product.id')->where(array("product_attribute.attr_value_id"=>$v['id'],"product.is_delete"=>0))->select();
			
			$v['product_idss'] = "";
			$arrays = array();
			foreach($products as $vps){
				array_push($arrays,$vps['product_id']);
			}
			if(count($arrays)>0){
				$v['product_idss'] =implode(",",$arrays);
			}
		}

		$this->assign('attributeValuesList', $attributeValuesList);
		//$this->assign('attribute_id', $attribute_id);
		//$this->assign('modelid', $modelid);
		$this->display('attribute_values_list');
	}
	//属性值添加
	public function attribute_values_add()
	{
		$attribute_id = intval($_GET['attrid']);
		$modelid = intval($_GET['modelid']);

		if(isset($_POST['dosubmit']))
		{
			$_POST['attr_id'] = $attribute_id;
			if (isset($_FILES['upimage']) && $_FILES['upimage']['tmp_name']){
				$info = $this->upload('attribute');
				if(is_array($info)) {// 上传成功 获取上传文件信息
					$_POST['attr_img'] = UPLOAD_PATH . $info['upimage']['savename'];
					if(isset($_FILES['upimage2']) && $_FILES['upimage2']['tmp_name']){
						$_POST['attr_img2'] = UPLOAD_PATH . $info['upimage2']['savename'];
					}
				}else{
					$this->error($info);
				}
			}elseif (isset($_FILES['upimage2']) && $_FILES['upimage2']['tmp_name']){
				$info = $this->upload('attribute');
				if(is_array($info)) {// 上传成功 获取上传文件信息
					$_POST['attr_img2'] = UPLOAD_PATH . $info['upimage2']['savename'];
				}else{
					$this->error($info);
				}
			}

			$attvaldb = D('attribute_values');
			$attvaldb->create();
			$attribute_values_id = $attvaldb->add();
			if($attribute_values_id)
			{
				//属性值内容详细入库
				AttributeModel::updateAttributeValuesItems($_POST['attrValue'], $attribute_values_id);
				$this->success('属性值添加成功！', U('attribute/attribute_values_list', array('modelid'=>$modelid,'attrid'=>$attribute_id)));
			}
			else
			{
				$this->error('属性值添加失败！', U('attribute/attribute_values_list', array('modelid'=>$modelid,'attrid'=>$attribute_id)));
			}
		}else{
			//属性
			$attribute = AttributeModel::getAttriubteInfo($attribute_id);
			
			$this->assign('attribute', $attribute);
			$this->assign('attribute_arr', $attribute_arr);
			$this->display('attribute_values_form');
		}
	}
	//编辑属性值
	public function attribute_values_edit()
	{
		$language_id = C('language_id');
		$attribute_id = intval($_GET['attrid']);
		$modelid = intval($_GET['modelid']);
		$attribute_values_id = intval($_GET['attvalid']);

		//验证是否存在
		$attvaldb = D('attribute_values');
		$attvalInfo = $attvaldb->find($attribute_values_id);
		if(is_null($attvalInfo))
		{
			$this->error('属性值不存在！', U('attribute/attribute_values_list', array('modelid'=>$modelid,'attrid'=>$attribute_id)));
		}

		if(isset($_POST['dosubmit']))
		{
			$_POST['attr_id'] = $attribute_id;
			if (isset($_FILES['upimage']) && $_FILES['upimage']['tmp_name']){
				$info = $this->upload('attribute');
				if(is_array($info)) {// 上传成功 获取上传文件信息
					$_POST['attr_img'] = UPLOAD_PATH . $info['upimage']['savename'];
					if(isset($_FILES['upimage2']) && $_FILES['upimage2']['tmp_name']){
						$_POST['attr_img2'] = UPLOAD_PATH . $info['upimage2']['savename'];
                        if (isset($_FILES['up_libu_img']) && $_FILES['up_libu_img']['tmp_name']){
                            $_POST['libu_img'] = UPLOAD_PATH . $info['up_libu_img']['savename'];
                        }
					} elseif (isset($_FILES['up_libu_img']) && $_FILES['up_libu_img']['tmp_name']){
                        $_POST['libu_img'] = UPLOAD_PATH . $info['up_libu_img']['savename'];
                    }
				}else{
					$this->error($info);
				}
			}elseif (isset($_FILES['upimage2']) && $_FILES['upimage2']['tmp_name']){
				$info = $this->upload('attribute');
				if(is_array($info)) {// 上传成功 获取上传文件信息
					$_POST['attr_img2'] = UPLOAD_PATH . $info['upimage2']['savename'];
                    if (isset($_FILES['up_libu_img']) && $_FILES['up_libu_img']['tmp_name']){
                        $_POST['libu_img'] = UPLOAD_PATH . $info['up_libu_img']['savename'];
                    }
				}else{
					$this->error($info);
				}
			} elseif (isset($_FILES['up_libu_img']) && $_FILES['up_libu_img']['tmp_name']){
                $info = $this->upload('attribute');
                $_POST['libu_img'] = UPLOAD_PATH . $info['up_libu_img']['savename'];
            }
			$attvaldb = D('attribute_values');
			$attvaldb->create();
		    $attvaldb->where('id='.$attribute_values_id)->save();
			if($attribute_values_id)
			{
				//属性值内容详细入库
				AttributeModel::updateAttributeValuesItems($_POST['attrValue'],$attribute_values_id);
				$this->success('属性值编辑成功！', U('attribute/attribute_values_list', array('modelid'=>$modelid,'attrid'=>$attribute_id)));
			}else{
				$this->error('属性值编辑失败！', U('attribute/attribute_values_list', array('modelid'=>$modelid,'attrid'=>$attribute_id)));
			}
		}else
		{
			$attvalInfo['items'] = AttributeModel::getAttributeItemsValues($attribute_values_id);

			//属性名称
			$attribute = AttributeModel::getAttriubteInfo($attribute_id);
			$this->assign('attribute', $attribute);
			$this->assign('attvalInfo', $attvalInfo);
            $this->assign('attribute_id', $attribute_id);
			$this->display('attribute_values_form');
		}
	}

//删除属性值
	public function attribute_values_delete()
	{
		$attribute_id = intval($_GET['attrid']);
		$attribute_values_id = intval($_GET['attvalid']);
		$modelid = intval($_GET['modelid']);
		AttributeModel::deleteAttriubteValues($attribute_values_id);
		$this->success('属性值删除成功！', U('attribute/attribute_values_list', array('modelid'=>$modelid,'attrid'=>$attribute_id)));
	}
	
	public function attribute_values_down(){
		$attribute_id = intval($_GET['attrid']);
		$attribute_values_id = intval($_GET['attvalid']);
		$modelid = intval($_GET['modelid']);
		$res=D('attribute_values')->where('id='.$attribute_values_id)->find();
		$is_down = ($res['is_down']==1)?2:1;
		if($res['is_down']==1){
			D('product_attribute')->where('attr_value_id='.$attribute_values_id)->delete();
		}
		D('attribute_values')->where('id='.$attribute_values_id)->save(array("is_down"=>$is_down));
		$this->success('属性下架成功！', U('attribute/attribute_values_list', array('modelid'=>$modelid,'attrid'=>$attribute_id)));
	}
	
	//ajax检验类型是否已经存在
	public function check_model_name_ajax()
	{
		$name = trim($_GET['name']);
		$modelid = intval($_GET['modelid']);
		$modeldb = D('model');
		$where['name'] = $name;
		if($modelid){
			$where['id'] = array('neq',$modelid);
		}
		$count = $modeldb->where($where)->count();
		if($count){
			exit('0');
		}
		exit('1');
	}
 //ajax检验属性是否已经存在
	public function check_attribute_name_ajax()
	{
		$name = trim($_GET['name']);
		$attrid = intval($_GET['attrid']);
		$attributedb = D('attribute');
		$where['name'] = $name;
		$where['model_id'] = $this->_modelid;
		if($attrid){
			$where['id'] = array('neq',$attrid);
		}
		$count = $attributedb->where($where)->count();
		if($count){
			exit('0');
		}
		exit('1');
	}
 //ajax检验属性值是否已经存在
	public function check_attribute_values_name_ajax()
	{
		$name = trim($_GET['name']);
		$attvalid = intval($_GET['attvalid']);
		$attrid = intval($_GET['attrid']);
		$attvaldb = D('attribute_values');
		$where['name'] = $name;
		$where['attribute_id'] = $attrid;
		if($attvalid){
			$where['id'] = array('neq',$attvalid);
		}
		$count = $attvaldb->where($where)->count();
		if($count){
			exit('0');
		}
		exit('1');
	}
	
	public function attribute_list_relation(){
		$modeldb = D('model');
	  	$modelList = $modeldb->field('model.*,count(attribute.attr_id) as model_count')->join('attribute ON attribute.model_id = model.id')->group('model.id')->select();
		$pid = I('get.modelid',intval);
	  	$this->assign('modelList', $modelList);
		$this->assign("name","版型列表");
		$this->assign("step",1);
		$this->assign('pid', $pid);
        $this->display('attribute_list_relation');
	}
	
	public function attribute_list_relation_1(){
		$modelid = intval($_GET['id']);
		$attributeList = AttributeModel::getAdminAttributeList($modelid);
		$length=count($attributeList);
		for($i=0;$i<$length;$i++){
			$attributeList[$i]['id'] = $attributeList[$i]['attr_id'];
		}
		$pid = I('get.pid',intval);
		$this->assign('modelList', $attributeList);
		$this->assign("name","选中");
		$this->assign("step",2);
		$this->assign('pid', $pid);
		$this->display('attribute_list_relation');
	}
	
	public function quick_add_attribute(){
		if((IS_AJAX && IS_POST)){
			try{
				$ajaxReturnData['code']=0;
				$ajaxReturnData['message']="传递数据异常，请重新操作！";
				$attribute_id 	= I('post.attr_id',intval);
				$pid 			= I('post.pid',intval);
				if(!$pid || !$attribute_id){
					$this->ajaxReturn($ajaxReturnData,'json');
				}
				$language_id = C('language_id');
				if(!$language_id){
					$language_id = 6;	
				}
				//添加新版型
				$attribute_msg=D('attribute')->where(array('attr_id'=>$attribute_id))->find();
				if(!$attribute_msg){
					$this->ajaxReturn($ajaxReturnData,'json');
				}
				$a_data['parent_id']		= $attribute_msg['parent_id'];
				$a_data['model_id']			= $pid;
				$a_data['attr_input_type']	= $attribute_msg['attr_input_type'];
				$a_data['attr_type']		= $attribute_msg['attr_type'];
				$a_data['attr_index']		= $attribute_msg['attr_index'];
				$a_data['attr_sell']		= $attribute_msg['attr_sell'];
				$a_data['sort_order']		= $attribute_msg['sort_order'];
				$a_data['attr_group']		= $attribute_msg['attr_group'];
				$a_data['attr_advanced']	= $attribute_msg['attr_advanced'];
				$a_data['attr_designer']	= $attribute_msg['attr_designer'];
				$a_data['attr_pro_type']	= $attribute_msg['attr_pro_type'];
				$a_data['attr_pro_fid']		= $attribute_msg['attr_pro_fid'];
				$a_data['attr_pro_sort']	= $attribute_msg['attr_pro_sort'];
				$a_data['attr_custom']		= $attribute_msg['attr_custom'];
				$a_data['size_type']		= $attribute_msg['size_type'];
				$a_data['orientation']		= $attribute_msg['orientation'];
				$a_data['issd']				= $attribute_msg['issd'];
				$a_data['is_ll']			= $attribute_msg['is_ll'];
				//$a_data['catgory']			= $attribute_msg['catgory'];
				if($attr_id=D('attribute')->add($a_data)){
					//添加版型语言表
					$attribute_items=D('attribute_items')->where(array('attribute_id'=>$attribute_id))->select();
					if(isset($attribute_items) && $attribute_items){
						foreach($attribute_items as $attribute_item){
							$new_item_data['attribute_id'] 	= $attr_id;
							$new_item_data['language_id'] 	= $attribute_item['language_id'];
							$new_item_data['name'] 			= $attribute_item['name'];
							$new_item_data['attr_content'] 	= $attribute_item['attr_content'];
							D('attribute_items')->add($new_item_data);
						}
					}
					//版型值列表
					$attvaldb = D('attribute_values');
					$attvaldb->join('attribute_values_items ON attribute_values_items.att_value_id = attribute_values.id');
					$attributeValuesList =$attvaldb->where('attr_id='.$attribute_id.' AND attribute_values_items.language_id='.$language_id)->order('sort_order DESC, id ASC')->select();
					foreach($attributeValuesList as $attribute){
						$attribute_value =D('attribute_values')->where(array('id'=>$attribute['id']))->find();
						$attribute_data['attr_id']	 	= $attr_id;
						$attribute_data['attr_img'] 	= $attribute_value['attr_img'];
						$attribute_data['attr_img2'] 	= $attribute_value['attr_img2'];
						$attribute_data['libu_img'] 	= $attribute_value['libu_img'];
						$attribute_data['sort_order'] 	= $attribute_value['sort_order'];
						$attribute_data['size_type'] 	= $attribute_value['size_type'];
						$attribute_data['orientation'] 	= $attribute_value['orientation'];
						$attribute_data['issd'] 		= $attribute_value['issd'];
						$attribute_data['button_type'] 	= $attribute_value['button_type'];
						$attribute_data['button_qty'] 	= $attribute_value['button_qty'];
						$attribute_data['add_money'] 	= $attribute_value['add_money'];
						if($last_id=D('attribute_values')->add($attribute_data)){
							$items=D('attribute_values_items')->where(array('att_value_id'=>$attribute['id']))->select();
							if(isset($items) && $items){
								foreach($items as $v){
									$item_data['att_value_id'] = $last_id;
									$item_data['language_id'] = $v['language_id'];
									$item_data['attr_value'] = $v['attr_value'];
									$item_data['attr_content'] = $v['attr_content'];
									//$item_data['attr_desc'] = $v['attr_desc'];
									D('attribute_values_items')->add($item_data);
								}
							}
							
						}
					}
					$ajaxReturnData['code']=200;
					$ajaxReturnData['message']="操作成功！";	
				}
			}catch (\Exception $e){
				$ajaxReturnData['code']=0;
				$ajaxReturnData['message']=$e->getMessage();
			}
		}else{
			$ajaxReturnData['code']=0;
			$ajaxReturnData['message']="数据异常，请重新操作！";
		}
		$this->ajaxReturn($ajaxReturnData,'json');	
	}
}